package com.dqhl.community.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.community.R;
import com.dqhl.community.adapter.ApplianceRepairTypeAdapter;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.ApplianceRepairType;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;
import com.dqhl.community.view.LuocangDialog;
import com.dqhl.community.view.MyGridView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * Created by liming on 2016/8/31.
 */
public class ApplianceRepairActivity extends BaseActivity implements View.OnClickListener {
    private Button btnAppoint;
    private ImageView ivTopBack;
    private TextView tvTopTitle;
    private LuocangDialog dialog;
    private EditText et_repairName, et_repairTel, et_repairAddress;
    private Context context;
    private ApplianceRepairType repairType;
    private List<ApplianceRepairType> repairTypeList;
    private MyGridView repair_myGridView;
    private String repair_ItemId = "";
    private TextView tv_locationCommunity;
    private TextView tv_RepairNoData;
    private static int REQUEST_CODE = 100;
    private String location = "";
    private String location_id = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appliance_repair);
        context = this;
        initView();
        //进入界面需先定位小区
        final LuocangDialog dialog = new LuocangDialog(ApplianceRepairActivity.this, "", "请先定位所在小区", "确认");
        dialog.setButtonClickListener(new LuocangDialog.ButtonClickListener() {
            @Override
            public void doConfirm() {
                dialog.dismiss();
                overlay(LocationClearActivity.class, REQUEST_CODE);
            }
        });
        dialog.show();

    }

    private void initView() {
        ivTopBack = (ImageView) findViewById(R.id.iv_top_back);
        ivTopBack.setVisibility(View.VISIBLE);
        tvTopTitle = (TextView) findViewById(R.id.tv_top_title);
        tvTopTitle.setText("家电维修");
        btnAppoint = (Button) findViewById(R.id.btn_appoint);
        et_repairName = (EditText) findViewById(R.id.et_repairName);
        et_repairTel = (EditText) findViewById(R.id.et_repairTel);
        et_repairAddress = (EditText) findViewById(R.id.et_repairAddress);
        repair_myGridView = (MyGridView) findViewById(R.id.repair_gridview);
        tv_locationCommunity = (TextView) findViewById(R.id.tv_cleanCommunity);
        tv_RepairNoData = (TextView) findViewById(R.id.tv_repair_nodata);
        btnAppoint.setOnClickListener(this);
        tv_locationCommunity.setOnClickListener(this);
        ivTopBack.setOnClickListener(this);

//        String communityName = spUserInfo.getString(Constant.COMMUNITY_ID, "定位所有小区");
//        tv_locationCommunity.setText(communityName);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.btn_appoint: //预约成功toast
                doCommit();
                break;
            //定位所在小区
            case R.id.tv_cleanCommunity:
                overlay(LocationRepairActivity.class, REQUEST_CODE);
                break;
        }
    }

    //  获取维修类型
    public void getRepairTyep() {
        showCustomLoadBar(R.string.loading);
        RequestParams params = new RequestParams(Config.Api.applianceClear_type);
        params.addBodyParameter("community_id",location_id);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(location_id+"GG维修类型"+result);
                int errCode = JsonUtils.getErrCode(result);
                if (errCode == 0) {
                    String data = JsonUtils.getInfo(result, "data");
                    repairTypeList = JSON.parseArray(data, ApplianceRepairType.class);
                    if (repairTypeList.size()>0) {
                        Dlog.e("GG维修类型返回List:" + data);
                        final ApplianceRepairTypeAdapter repairTypeAdapter = new ApplianceRepairTypeAdapter(ApplianceRepairActivity.this, repairTypeList);
                        tv_RepairNoData.setVisibility(View.GONE);
                        repair_myGridView.setVisibility(View.VISIBLE);
                        repair_myGridView.setAdapter(repairTypeAdapter);
                        repair_myGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                                ApplianceRepairType repairType = (ApplianceRepairType) adapterView.getItemAtPosition(position);
                                Dlog.e("GG维修返回的ID " + repairType.getId());
                                repair_ItemId = repairType.getId();
                                repairTypeAdapter.clearSelection(position);
                                repairTypeAdapter.notifyDataSetChanged();
                            }
                        });
                    }else {
                        repair_myGridView.setVisibility(View.GONE);
                        tv_RepairNoData.setVisibility(View.VISIBLE);
                        tv_RepairNoData.setText(R.string.error_no_info);
                    }

                }else{
                    tv_RepairNoData.setVisibility(View.VISIBLE);
                    tv_RepairNoData.setText(R.string.error_server_connection);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                tv_RepairNoData.setVisibility(View.VISIBLE);
                tv_RepairNoData.setText(R.string.error_net_connection);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    //提交案件
    public void doCommit() {
        String repairName = et_repairName.getText().toString();
        String repairTel = et_repairTel.getText().toString();
        String repairAddress = et_repairAddress.getText().toString();
        Dlog.e("GGrepair_ItemId" + repair_ItemId);
        if ("".equals(repairName)) {
            toast("姓名不能为空");
        } else if ("".equals(repairTel)) {
            toast("电话不能为空");
        } else if ("".equals(repairAddress)) {
            toast("地址不能为空");
        } else if ("".equals(repair_ItemId)) {
            toast("请选择清洗项目");
        } else if ("".equals(location)) {
            toast("请选择所在小区");
        } else {
            showCustomLoadBar(R.string.loading);
            RequestParams params = new RequestParams(Config.Api.applianceClear);
            params.addBodyParameter("name", repairName);
            params.addBodyParameter("phone", repairTel);
            params.addBodyParameter("address", repairAddress);
            params.addBodyParameter("project_id", repair_ItemId);
            params.addBodyParameter("userid", app.getUserId());
            params.addBodyParameter("location", location);
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    int errCode = JsonUtils.getErrCode(result);
                    Dlog.e(errCode + "GG家电维修返回" + result);
                    String data = JsonUtils.getInfo(result, "data");
                    if (errCode == 0) {
                        dialog = new LuocangDialog(ApplianceRepairActivity.this, "预约成功！", "我们会尽快处理", "确认");
                        dialog.setButtonClickListener(new LuocangDialog.ButtonClickListener() {
                            @Override
                            public void doConfirm() {
                                dialog.dismiss();
                                overlay(ApplianceRepairRecordActivity.class);
                            }
                        });
                        dialog.show();
                    }else{
                        hideCustomLoadBar();
                        toast("预约失败");
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    hideCustomLoadBar();
                    toast(R.string.error_net_connection);
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {
                    hideCustomLoadBar();
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
                Bundle bundle = data.getExtras();
                Dlog.e( bundle.getString("community_id")+"GG返回的数据" + bundle.getString("community_name"));
                location = bundle.getString("community_name");
                location_id = bundle.getString("community_id");
                tv_locationCommunity.setText(bundle.getString("community_name"));
                getRepairTyep();
            }
        }

    }


}
